<?php

namespace app\admin\controller;
use think\Db;
use app\common\controller\Backend;

/**
 * 用户
 *
 * @icon fa fa-user
 */
class Statistics extends Backend
{

    protected $noNeedRight = ['statistics','selecttime'];
    /**
     * Partner模型对象
     * @var \app\admin\model\Partner
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    //统计
    public function index()
    {
        $storecount=db('store')->count();
        $storecount1=db('store')->where(['status'=>'3'])->count();
        $carcount=db('car')->count();
        $carcount1=db('car')->where(['status'=>'3'])->count();
        $productcount=db('product')->count();
        $productcount1=db('product')->where(['status'=>'3'])->count();
        $violationcount=db('violation')->count();
        $violationcount1=db('violation')->where(['is_illegal'=>'1'])->count();
        $this->assign('storecount',$storecount);
        $this->assign('storecount1',$storecount1);
        $this->assign('carcount',$carcount);
        $this->assign('carcount1',$carcount1);
        $this->assign('productcount',$productcount);
        $this->assign('productcount1',$productcount1);
        $this->assign('violationcount',$violationcount);
        $this->assign('violationcount1',$violationcount1);
        return $this->view->fetch();
    }
    //工作台根据时间搜索
    public function selecttime()
    {
        $selectTime = $this->request->param('selecttime');
        $where_store = "";
        $where_car = "";
        $where_product = "";
        $where_violation = "";
        $whereSUC = "";
        if (!empty($selectTime)) {
            $selectTimeArr = explode(' - ',$selectTime);
            $startTime = "";
            $endTime = "";
            if (isset($selectTimeArr[0])) {
                $startTime = strtotime($selectTimeArr[0]);
            }
            if (isset($selectTimeArr[1])) {
                $endTime = strtotime($selectTimeArr[1]);
            }

            if (!empty($startTime)) {
                $where_store.="  add_time >= '{$startTime}'";
                $where_car.="  add_time >= '{$startTime}'";
                $where_product.="  add_time >= '{$startTime}'";
                $where_violation.="  add_time >= '{$startTime}'";
            }

            if (!empty($endTime)) {
                $where_store.=" AND add_time < '{$endTime}'";
                $where_car.=" AND add_time < '{$endTime}'";
                $where_product.=" AND add_time < '{$endTime}'";
                $where_violation.=" AND add_time < '{$endTime}'";
            }
        }
        
        $storecount=db('store')->where($where_store)->count();
        $storecount1=db('store')->where(['status'=>'3'])->where($where_store)->count();
        $carcount=db('car')->where($where_car)->count();
        $carcount1=db('car')->where(['status'=>'3'])->where($where_car)->count();
        $productcount=db('product')->where($where_product)->count();
        $productcount1=db('product')->where(['status'=>'3'])->where($where_product)->count();
        $violationcount=db('violation')->where($where_violation)->count();
        $violationcount1=db('violation')->where(['is_illegal'=>'1'])->where($where_violation)->count();

        return json([
            'code'=>1,
            'msg'=>'获取成功',
            'list'=>[
                'storecount'=>$storecount,
                'storecount1'=>$storecount1,
                'carcount'=>$carcount,
                'carcount1'=>$carcount1,
                'productcount'=>$productcount,
                'productcount1'=>$productcount1,
                'violationcount'=>$violationcount,
                'violationcount1'=>$violationcount1,
            ]
        ]);
        

    }


}
